package com.comsoftstar.nt5s.app;

import android.app.Application;
import android.content.Context;

import com.hzc.imagepicker.PickerConfig;
import com.hzc.imagepicker.SImagePicker;
import com.hzc.nt5s.R;
import com.hzc.nt5s.imageloader.FrescoImageLoader;

import org.xutils.DbManager;
import org.xutils.x;

/**
 * Created by Martin on 2017/1/17.
 */

public class PickerApplication extends Application {

  private static Application instance;
  public static DbManager.DaoConfig daoConfig;
  @Override
  public void onCreate() {
    super.onCreate();
    instance = this;
    SImagePicker.init(new PickerConfig.Builder().setAppContext(this)
        .setImageLoader(new FrescoImageLoader())
        .setToolbaseColor(getResources().getColor(R.color.colorPrimary)).build());
    //初始化xutils;
    initUtils();
    //初始化数据库
//    initDataBase();
  }
  private void initDataBase() {
    daoConfig = new DbManager.DaoConfig()
            .setDbName("NTJF.db")
            // 不设置dbDir时, 默认存储在app的私有目录.
//                .setDbDir(new File("/sdcard/Android/data/com.nantai.jiafang.nantaijiafang/files")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了.
            .setDbVersion(6)
            .setDbOpenListener(new DbManager.DbOpenListener() {
              @Override
              public void onDbOpened(DbManager db) {
                // 开启WAL, 对写入加速提升巨大
                db.getDatabase().enableWriteAheadLogging();
              }
            })
            .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
              @Override
              public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                //: ...

//                        String save = Tools.getSharedValue(MyApplication.this, IConstant.FILE_USER, IConstant.SP_ISPWD_REMEMBER);
//                        if (save.equals("1")) {
//                            try {
//                                db.update(ProductionOrderInfo.class,
//                                        String.valueOf(new KeyValue("userId", Tools.getSharedValue(MyApplication.this, IConstant.FILE_USER, IConstant.SP_LOGIN_NAME))));
//                                db.update(FabricBatchInfo.class,
//                                        String.valueOf(new KeyValue("userId", Tools.getSharedValue(MyApplication.this, IConstant.FILE_USER, IConstant.SP_LOGIN_NAME))));
//                            } catch (DbException e) {
//                                e.printStackTrace();
//                            }
//
//
//                        }

                //db.addColumn("Timestamp");
                // db.dropTable(...);
                // ...
                // or
                // db.dropDb();
              }
            });
  }

  private void initUtils() {
    x.Ext.init(this);
    x.Ext.setDebug(false); // 是否输出debug日志, 开启debug会影响性能.
  }

  public static Context getAppContext() {
    return instance;
  }
}
